﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using TSOL.FastSPWeb.Core;


namespace TSOL.FastSPWeb
{
    public partial class FastSPWebCall
    {
        static void internalOpenWeb(Action<EventWaitHandle, FastSPWebGeneralException> openProc)
        {
            EventWaitHandle wh = new ManualResetEvent(false);
            FastSPWebGeneralException SPWebException = new FastSPWebGeneralException();
            try
            {

                openProc(wh, SPWebException);
                
                wh.WaitOne();
                if (SPWebException.Get() != null)
                    throw new SPException("FastSPWebCall.OpenWeb Exception", SPWebException.Get());

            }
            finally
            {
                wh.Close();
            }
        }

        public static void OpenWeb(Guid inSiteID, Guid inWebID, Action<SPSite, SPWeb, string> openProc)
        {
            internalOpenWeb((eventWaitHandle, SPWebException) =>
            {
                FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.QueueOpenWeb(
                        inSiteID, inWebID, (FastWebSite, FastWebweb, FastWebhashCode) =>
                        {
                            try
                            {
                                openProc(FastWebSite, FastWebweb, FastWebhashCode);
                            }
                            catch (Exception ex)
                            {
                                SPWebException.Set(ex);
                            }
                            finally
                            {
                                eventWaitHandle.Set();
                            }
                        });
            });

        }

        public static void OpenWeb(SPUser user, Guid inSiteID, Guid inWebID, Action<SPSite, SPWeb, string> openProc)
        {
            internalOpenWeb((eventWaitHandle, SPWebException) =>
            {
                FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.QueueOpenWeb(
                        user, inSiteID, inWebID, (FastWebSite, FastWebweb, FastWebhashCode) =>
                        {
                            try
                            {
                                openProc(FastWebSite, FastWebweb, FastWebhashCode);
                            }
                            catch (Exception ex)
                            {
                                SPWebException.Set(ex);
                            }
                            finally
                            {
                                eventWaitHandle.Set();
                            }
                        });
            });

        }
        public static void OpenWeb(string inSiteUrl, string inWebUrl, Action<SPSite, SPWeb, string> openProc)
        {
            internalOpenWeb((eventWaitHandle, SPWebException) =>
            {
                FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.QueueOpenWeb(
                        inSiteUrl, inWebUrl, (FastWebSite, FastWebweb, FastWebhashCode) =>
                        {
                            try
                            {
                                openProc(FastWebSite, FastWebweb, FastWebhashCode);
                            }
                            catch (Exception ex)
                            {
                                SPWebException.Set(ex);
                            }
                            finally
                            {
                                eventWaitHandle.Set();
                            }
                        });
            });
        }
        public static void OpenWeb(SPUser user, string inSiteUrl, string inWebUrl, Action<SPSite, SPWeb, string> openProc)
        {
            internalOpenWeb((eventWaitHandle, SPWebException) =>
            {
                FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.QueueOpenWeb(
                        user, inSiteUrl, inWebUrl, (FastWebSite, FastWebweb, FastWebhashCode) =>
                        {
                            try
                            {
                                openProc(FastWebSite, FastWebweb, FastWebhashCode);
                            }
                            catch (Exception ex)
                            {
                                SPWebException.Set(ex);
                            }
                            finally
                            {
                                eventWaitHandle.Set();
                            }
                        });
            });
        }
    }

}
